﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using System.Collections.ObjectModel;
using System.ComponentModel;

namespace HomeWP
{
    public partial class Variables : PhoneApplicationPage
    {
        public Variables()
        {
            InitializeComponent();

            // Create the database if it does not exist.
            using (MainPage.HMDataContext db = new MainPage.HMDataContext(MainPage.HMDataContext.DBConnectionString))
            {
                if (db.DatabaseExists() == false)
                {
                    //Create the database
                    db.CreateDatabase();
                }
            }

            // Connect to the database and instantiate data context.
            hMDB = new MainPage.HMDataContext(MainPage.HMDataContext.DBConnectionString);

            if (hMDB.HMCCUSettings.Count(t => t.ConfigID == "TEST") > 0)
            {
                CCUSet = hMDB.HMCCUSettings.Single(t => t.ConfigID == "TEST");
            }

            // Data context and observable collection are children of the main page.
            this.DataContext = this;
        }

        private MainPage.HMDataContext hMDB;
        private CCUSettings CCUSet;

        private ObservableCollection<SystemVariable> _hMVariables;
        public ObservableCollection<SystemVariable> HMVariables
        {
            get
            {
                return _hMVariables;
            }
            set
            {
                if (_hMVariables != value)
                {
                    _hMVariables = value;
                    NotifyPropertyChanged("HMVariable");
                }
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify Silverlight that a property has changed.
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            HMVariables = new ObservableCollection<SystemVariable>(from SystemVariable oItem in hMDB.HMVariables_ select oItem);
            spVars.Children.Clear();
            
            foreach (SystemVariable oVar in HMVariables.Where(t => t.Visible == true))
            {
                VariableControl VarCtr = new VariableControl();
                //VarCtr.HMVar = oVar;
                VarCtr.DataContext = oVar;
                spVars.Children.Add(VarCtr);
                spVars.Children.Add(new HMSeparator());
            }
        }
    }
}